package com.lzy.system.dictManage.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lzy.conf.JSONResult;
import com.lzy.system.dictManage.dto.SysDicValueDto;
import com.lzy.system.dictManage.dto.SysDicValueUpdateDto;
import com.lzy.system.dictManage.dto.convert.SysDicValueConvert;
import com.lzy.system.dictManage.entity.SysDicValue;
import com.lzy.system.dictManage.service.ISysDicValueService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import java.util.List;

/**
 * <p>
 * 字典数据 前端控制器
 * </p>
 *
 * @author lcc
 * @since 2024-01-05
 */
@RestController
@RequestMapping("/dictManage/sys-dic-value")
@Api(tags = "字典数据")
@Validated
public class SysDicValueController {

    private ISysDicValueService iSysDicValueService;
    private SysDicValueConvert sysDicValueConvert;

    @Autowired
    public SysDicValueController(ISysDicValueService iSysDicValueService, SysDicValueConvert sysDicValueConvert) {
        this.iSysDicValueService = iSysDicValueService;
        this.sysDicValueConvert = sysDicValueConvert;
    }

    @RequestMapping(value = "/getList", method = RequestMethod.POST)
    @ApiOperation("获取字典数据列表")
    public JSONResult getList(
            @ApiParam("分组id") @RequestParam(name = "group_id") @NotBlank(message = "分组id必须填写") String group_id,
            @ApiParam("每页显示行数") @RequestParam(name = "limit", required = true, defaultValue = "10") int limit,
            @ApiParam("当前页数") @RequestParam(name = "page", required = true, defaultValue = "1") int page
    ) {
        IPage<SysDicValue> page_obj = iSysDicValueService.page(new Page<SysDicValue>(page, limit), new QueryWrapper<>() {{
            eq("group_id", group_id);
        }});
        return JSONResult.ok(page_obj);
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ApiOperation("添加字典数据")
    public JSONResult add(
            @ApiParam("字典数据") @RequestBody @Validated SysDicValueDto sysDicValueDto
    ) {
        SysDicValue sysDicValue = sysDicValueConvert.addDtoToEntity(sysDicValueDto);
        iSysDicValueService.save(sysDicValue);
        return JSONResult.ok();
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ApiOperation("修改字典数据")
    public JSONResult update(
            @ApiParam("字典数据") @RequestBody @Validated SysDicValueUpdateDto sysDicValueUpdateDto
    ) {
        SysDicValue sysDicValue = sysDicValueConvert.updateDtoToEntity(sysDicValueUpdateDto);
        iSysDicValueService.updateById(sysDicValue);
        return JSONResult.ok();
    }

    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ApiOperation("删除字典分组")
    public JSONResult delete(
            @ApiParam("ids") @RequestBody @Validated List<String> ids
    ) {
        iSysDicValueService.removeByIds(ids);
        return JSONResult.ok();
    }
}
